const app = getApp();
Page({
    data: {
      navigator: { hide: 0 }
    },
    onLoad: function(t) {
      var that =this
      if (!app.globalData.userInfo) {
        app.getUserInfo();
      }
      this.setData({
        userInfo: app.globalData.userInfo,
      })
      //屏幕宽度
      wx.getSystemInfo({
        success: function (res) {
          var width = res.screenWidth;
          var v = that.data.navigator;
          v.width = width
          that.setData({
            navigator: v
          })
        },
      })

      //实例化一个动画
      this.animation = wx.createAnimation({
        // 动画持续时间，单位ms，默认值 400
        duration: 300,
        /**
         * linear 动画一直较为均匀
         * ease  从匀速到加速在到匀速
         * ease-in 缓慢到匀速
         * ease-in-out 从缓慢到匀速再到缓慢
         * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
         * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
         */
        timingFunction: 'ease',
        // 延迟多长时间开始
        delay: 100,
        /**
         * 以什么为基点做动画 效果自己演示
         * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
         * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
         */
        transformOrigin: 'right',
        success: function (res) {

        }
      })
      let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
      this.setData({
        token: extConfig.appid,
        openid: app.globalData.openid
      })
      var width = this.data.navigator.width;
      var offset = width * 0.64;
      that.animation.translateX(offset).step()
      var v = this.data.navigator
      v.animation = that.animation.export()
      v.hide = 1
      that.setData({
        //输出动画
        navigator: v
      })
      //获取我的数据
      wx.request({
        url: 'https://www.58zltc.com/api.php/Index/getMyPageInfo?openid=' + app.globalData.openid + '&token=' + that.data.token,
        success: res => {
          that.setData({
            pageAllInfo: res.data,
          })
        }
      })
    },
    backto: function (e) {
      wx.navigateBack({})
    },
    //快捷导航
    navigator: function (e) {
      var width = this.data.navigator.width
      if (this.data.navigator.hide == 1) {
        var offset = '0'
        var mum = 0;
      } else {
        var offset = width * 0.64
        var mum = 1;
      }
      this.animation.translateX(offset).step()
      var v = this.data.navigator
      v.animation = this.animation.export();
      v.hide = mum;
      this.setData({
        //输出动画
        navigator: v
      })
    },
    formsubmit:function(e){
      //添加个模板id
      app.addFormId(e.detail.formId);
      var post_data = e.detail.value;
      var that = this;
      post_data.openid = app.globalData.openid;
      this.setData({
        post_data: post_data
      })
      app.ihttpPost('https://www.58zltc.com/api.php/Commission/become_leader', post_data, function (res) {
        var res = res.data
        if(res.err_code ==400){
          wx.showModal({
            title: '提示',
            cancelText:'暂不',
            content: res.err_msg+'您还可以通过支付成为队长，是否支付？',
            cancelColor:'#1c1c1e',
            confirmColor:'#ff4d49',
            success:function(e){
              //点击确认，花钱成为队长
              if (e.confirm){
                app.ihttpPost('https://www.58zltc.com/api.php/Commission/getPayOrder', post_data, function (res) {
                  var data = res.data;
                  var prepay_id = res.data.package;
                  var a = prepay_id.slice(10);
                  //支付添加formid
                  app.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: a, openid: app.globalData.openid, type: 'pay' }, function (res) { })
                  wx.requestPayment({
                    'appId': res.data.appId,
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.paySign,
                    'success': res => {
                      if (res.errMsg == 'requestPayment:ok') {
                        wx.showToast({
                          icon: 'none',
                          title: '申请成功，请耐心等待审核！',
                        })
                        setTimeout(function () {
                          wx.navigateBack({})
                        }, 1500)
                      }
                    }
                  })
                })
              }
            }
          })
        }
        //脱离团队成为队长
        else if (res.err_code == 200) {
          wx.showModal({
            title: '提示',
            content: res.err_msg,
            success: function (e) {
              if (e.confirm) {
                that.become_leader2();
              }
            }
          })
        }
        else if (res.err_code == 401){
          wx.showModal({
            title: '提示',
            content: res.err_msg,
            showCancel:false, 
          })
        }
        else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.err_msg,
            confirmColor: '#1c1c1e'
          })
        }
      })
    },
    become_leader2:function(){
      var post_data = this.data.post_data;
      app.ihttpPost('https://www.58zltc.com/api.php/Commission/become_leader2', post_data, function (res) {
        var res = res.data
        if (res.err_code == 400) {
          wx.showModal({
            title: '提示',
            cancelText: '暂不',
            content: res.err_msg + '您还可以通过支付成为队长，是否支付？',
            cancelColor: '#1c1c1e',
            confirmColor: '#ff4d49',
            success: function (e) {
              //点击确认，花钱成为队长
              if (e.confirm) {
                app.ihttpPost('https://www.58zltc.com/api.php/Commission/getPayOrder', post_data, function (res) {
                  var data = res.data;
                  var prepay_id = res.data.package;
                  var a = prepay_id.slice(10);
                  //支付添加formid
                  app.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: a, openid: app.globalData.openid, type: 'pay' }, function (res) { })
                  wx.requestPayment({
                    'appId': res.data.appId,
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.paySign,
                    'success': res => {
                      if (res.errMsg == 'requestPayment:ok') {
                        wx.showToast({
                          icon: 'none',
                          title: '申请成功，请耐心等待审核！',
                        })
                        setTimeout(function () {
                          wx.navigateBack({})
                        }, 1500)
                      }
                    }
                  })
                })
              }
            }
          })
        }
        else if (res.err_code == 401) {
          wx.showModal({
            title: '提示',
            content: res.err_msg,
            showCancel: false,
          })
        }
        else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.err_msg,
            confirmColor: '#1c1c1e'
          })
        }
      })
    }
});